//
//  LoginViewController.m
//  taishan
//
//  Created by mac on 2019/4/8.
//  Copyright © 2019年 jznrj. All rights reserved.
//

#import "LoginViewController.h"
#import <Masonry.h>
#import "SizeUtil.h"
#import "ColorUtil.h"
#import "URLUtil.h"
#import "JZNRequestUtil.h"
#import "Toast+UIView.h"
#import <MJExtension.h>
#import <MJRefresh.h>
#import "TaishanRegisterViewController.h"
#import "TabViewController.h"
const float textLoginFont = 12;
const float buttonLoginFont = 13;

const float viewHeight = 45;
const float contentHeight = 30;

@interface LoginViewController ()<UITextFieldDelegate>
@property(nonatomic, strong) UIImageView *logoImageView;
/**密码*/
@property(nonatomic, strong) UILabel *pwdLabel;
/**账号*/
@property(nonatomic, strong) UILabel *accountLabel;
/**密码输入框*/
@property(nonatomic, strong) UITextField *pwdTextField;

/**密码输入框*/
@property(nonatomic, strong) UITextField *first;
/**账号输入框*/
@property(nonatomic, strong) UITextField *accountTextField;

/**登录按钮*/
@property(nonatomic, strong) UIButton *loginButton;

/**登录按钮*/
@property(nonatomic, strong) UIButton *registerButton;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initView];
    [self registerEnable];
   // [[UIApplication sharedApplication].keyWindow addSubview:self.view];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
# pragma mark ====方法====
-(void) registerEnable{
    NSString *url = [NSString stringWithFormat:@"%@/account/findRegistEnable" ,[URLUtil hostName]];
    [JZNRequestUtil requestUrl:url requestParams:nil success:^(DataResult *dataResult) {
        if(dataResult.code == 0){
            self.registerButton.hidden = NO;
        }else {
            self.registerButton.hidden = YES;
            [[UIApplication sharedApplication].delegate.window makeToast:dataResult.msg];
        }
    } failure:^(NSError *error) {
        
    }];
}

-(void) regist {
    TaishanRegisterViewController *registerViewController = [[TaishanRegisterViewController alloc]init];
    [UIApplication sharedApplication].delegate.window.rootViewController = registerViewController;
}

-(void) login{
    __block NSString *accountString = self.accountTextField.text;
    __block NSString *pwdString = self.pwdTextField.text;
    
    NSString *url = [NSString stringWithFormat:@"%@/user/userLogin" ,[URLUtil hostName]];
    NSDictionary *params = @{@"userMobile":accountString,
                             @"password":pwdString,
                             @"businessCompanyId":@"1"
                             };
    [JZNRequestUtil requestUrl:url requestParams:params success:^(DataResult *dataResult) {
        if(dataResult.code == 0){
            NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
            [userDefault setObject:accountString forKey:@"account"];
            [userDefault setObject:pwdString forKey:@"pwd"];
            [userDefault setObject:dataResult.token forKey:@"token"];
            [userDefault setObject:[dataResult.data mj_JSONString] forKey:@"userInfo"];
            
            TabViewController *tb = [[TabViewController alloc]init];
            tb.selectedIndex = _tabIndex;
            [UIApplication sharedApplication].delegate.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
            [UIApplication sharedApplication].delegate.window.rootViewController = tb;
            [[UIApplication sharedApplication].delegate.window makeKeyAndVisible];
            [self dismissViewControllerAnimated:NO completion:nil];
        }else {
         [[UIApplication sharedApplication].delegate.window makeToast:dataResult.msg];
        }
    } failure:^(NSError *error) {
        
    }];
}

# pragma mark ====页面控件====
-(void) initView{
    

    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.logoImageView];
    [self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(100);
        make.centerX.equalTo(self.view);
        if (@available(iOS 11.0, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(50);
        }else {
            make.top.equalTo(self.view).offset(50);
        }
    }];
    
    [self.view addSubview:self.accountLabel];
    [self.accountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view).offset(-50*LSCALE);
        make.height.mas_equalTo(10*LSCALE);
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.logoImageView.mas_bottom).offset(50);
    }];
    
    [self.view addSubview:self.accountTextField];
    [self.accountTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view).offset(-50*LSCALE);
        make.height.mas_equalTo(contentHeight*LSCALE);
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.accountLabel.mas_bottom).offset(5);
    }];
    
    UIImageView *loginLine = [[UIImageView alloc]init];
    loginLine.backgroundColor = [UIColor darkGrayColor];
    [self.view addSubview:loginLine];
    [loginLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view).offset(-50*LSCALE);
         make.height.mas_equalTo(1);
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.accountTextField.mas_bottom);
    }];
    
    [self.view addSubview:self.pwdLabel];
    [self.pwdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view).offset(-50*LSCALE);
        make.height.mas_equalTo(10*LSCALE);
        make.centerX.equalTo(self.view);
        make.top.equalTo(loginLine.mas_bottom).offset(10);
    }];
    [self.view addSubview:self.pwdTextField];
    [self.pwdTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view).offset(-50*LSCALE);
        make.height.mas_equalTo(contentHeight*LSCALE);
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.pwdLabel.mas_bottom).offset(5);
    }];
    UIImageView *pwdLine = [[UIImageView alloc]init];
    pwdLine.backgroundColor = [UIColor darkGrayColor];
    [self.view addSubview:pwdLine];
    [pwdLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.view).offset(-50*LSCALE);
        make.height.mas_equalTo(1);
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.pwdTextField.mas_bottom);
    }];

    [self.view addSubview:self.loginButton];
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30*LSCALE);
        make.width.equalTo(self.view).offset(-50*LSCALE);
        make.centerX.equalTo(self.view);
        make.top.equalTo(pwdLine.mas_bottom).offset(20);
    }];
    
    [self.view addSubview:self.registerButton];
    self.registerButton.hidden = NO;
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30*LSCALE);
        make.width.equalTo(self.view).offset(-50*LSCALE);
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.loginButton.mas_bottom).offset(20);
    }];
}

- (BOOL)textFieldShouldReturn:(UITextField*)textField{

    [textField resignFirstResponder];

    return YES;

}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    BOOL no = textField.window.isKeyWindow;
    if (!textField.window.isKeyWindow) {
       
    }
}

# pragma mark ====控件====
-(UIImageView*) logoImageView{
    if (!_logoImageView) {
        _logoImageView = [[UIImageView alloc]init];
        _logoImageView.image = [UIImage imageNamed:@"online_logo.png"];
    }
    return _logoImageView;
}

-(UILabel*) pwdLabel{
    if (!_pwdLabel) {
        _pwdLabel = [[UILabel alloc]init];
        _pwdLabel.text = @"密码";
        _pwdLabel.textColor = [UIColor blackColor];
        _pwdLabel.font = [UIFont systemFontOfSize:10*LSCALE];
    }
    return _pwdLabel;
}

-(UITextField*) pwdTextField{
    if (!_pwdTextField) {
        _pwdTextField = [[UITextField alloc]init];
        //        _pwdTextField.placeholder = @"6-16位字母、数字、符号组成";
        _pwdTextField.font = [UIFont systemFontOfSize:textLoginFont*LSCALE];
        _pwdTextField.textColor = [UIColor blackColor];
        _pwdTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入密码" attributes:@{NSForegroundColorAttributeName: [UIColor darkGrayColor]}];
        _pwdTextField.delegate = self;
        _pwdTextField.secureTextEntry = YES;
    }
    return _pwdTextField;
}

-(UILabel*) accountLabel{
    if (!_accountLabel) {
        _accountLabel = [[UILabel alloc]init];
        _accountLabel.text = @"账号";
        _accountLabel.textColor = [UIColor blackColor];
        _accountLabel.font = [UIFont systemFontOfSize:10*LSCALE];
    }
    return _accountLabel;
}

-(UITextField*) first {
    if (!_first) {
         _first = [[UITextField alloc]init];
         //        _accountTextField.placeholder = @"请输入手机号";
         _first.font = [UIFont systemFontOfSize:textLoginFont*LSCALE];
         _first.textColor = [UIColor blackColor];
         _first.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入账号" attributes:@{NSForegroundColorAttributeName: [UIColor darkGrayColor]}];
          _first.delegate = self;
         // _accountTextField.keyboardType = UIKeyboardTypeNumberPad;
     }
     return _first;
}

-(UITextField*) accountTextField{
    if (!_accountTextField) {
        _accountTextField = [[UITextField alloc]init];
        //        _accountTextField.placeholder = @"请输入手机号";
        _accountTextField.font = [UIFont systemFontOfSize:textLoginFont*LSCALE];
        _accountTextField.textColor = [UIColor blackColor];
        _accountTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入账号" attributes:@{NSForegroundColorAttributeName: [UIColor darkGrayColor]}];
         _accountTextField.delegate = self;
         
    }
    return _accountTextField;
}
-(UIButton*) loginButton{
    if (!_loginButton) {
        _loginButton = [[UIButton alloc]init];
        _loginButton.backgroundColor = [ColorUtil titleColor];
        _loginButton.titleLabel.font = [UIFont systemFontOfSize:buttonLoginFont*LSCALE];
        _loginButton.titleLabel.textColor = [UIColor whiteColor];
        _loginButton.layer.cornerRadius = 5;
        [_loginButton setTitle:@"登  录" forState:UIControlStateNormal];
        [_loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginButton;
}

-(UIButton*) registerButton{
    if (!_registerButton) {
        _registerButton = [[UIButton alloc]init];
        _registerButton.backgroundColor = [ColorUtil titleColor];
        _registerButton.titleLabel.font = [UIFont systemFontOfSize:buttonLoginFont*LSCALE];
        _registerButton.titleLabel.textColor = [UIColor whiteColor];
        _registerButton.layer.cornerRadius = 5;
        [_registerButton setTitle:@"注  册" forState:UIControlStateNormal];
        [_registerButton addTarget:self action:@selector(regist) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registerButton;
}
@end
